package it.avantsoft.ls.ui;

import it.avantsoft.ls.base.ApplicationTest;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class GuiMenu extends JMenuBar {
    String[] fileItems = new String[]{"Print All AddressBook", "Kill Peer", "Check Peer Connection"};
    String[] editItems = new String[]{"Undo", "Cut", "Copy", "Paste"};
    char[] fileShortcuts = {'P', 'K', 'C', 'X'};
    char[] editShortcuts = {'Z', 'X', 'H', 'V'};

    public GuiMenu() {
        JMenu testMenu = new JMenu("Test");
        JMenu editMenu = new JMenu("Edit");
        JMenu otherMenu = new JMenu("Other");
        JMenu subMenu = new JMenu("SubMenu");
        JMenu subMenu2 = new JMenu("SubMenu2");

        // Assemble the File menus with mnemonics.
        ActionListener printListener = new ActionListener() {
            public void actionPerformed(ActionEvent event) {
                if (event.getActionCommand() != null) {
                    if (event.getActionCommand().equals(fileItems[0])) {
                        //Print All AddressBook
                        ApplicationTest.form.printAllAddressBook();
                    } else if (event.getActionCommand().equals(fileItems[1])) {
                        //Kill Peer
                        String peerNumber = JOptionPane.showInputDialog(null, "Peer number? (0:Server)");
                        ApplicationTest.kill(Integer.parseInt(peerNumber));
                    } else if (event.getActionCommand().equals(fileItems[2])) {
                        //Check Peer Connection
                        String peerNumber = JOptionPane.showInputDialog(null, "Peer number? (0:Server)");
                        String peerName = ApplicationTest.getApplications().get(Integer.parseInt(peerNumber) - 1).getPeerHandler().getPeerId();
                        ApplicationTest.form.addSimpleString("Peer " + peerName + " connected = " + String.valueOf(ApplicationTest.peerConnected(Integer.parseInt(peerNumber))));
                    }
                }
            }
        };
        for (int i = 0; i < fileItems.length; i++) {
            JMenuItem item = new JMenuItem(fileItems[i]);
            item.setAccelerator(KeyStroke.getKeyStroke(fileShortcuts[i],
                    Toolkit.getDefaultToolkit().getMenuShortcutKeyMask(), false));
            item.addActionListener(printListener);
            testMenu.add(item);
        }

        // Assemble the File menus with keyboard accelerators.
        for (int i = 0; i < editItems.length; i++) {
            JMenuItem item = new JMenuItem(editItems[i]);
            item.setAccelerator(KeyStroke.getKeyStroke(editShortcuts[i],
                    Toolkit.getDefaultToolkit().getMenuShortcutKeyMask(), false));
            item.addActionListener(printListener);
            editMenu.add(item);
        }

        // Insert a separator in the Edit menu in Position 1 after "Undo".
        editMenu.insertSeparator(1);

        // Assemble the submenus of the Other menu.
        JMenuItem item;
        subMenu2.add(item = new JMenuItem("Extra 2"));
        item.addActionListener(printListener);
        subMenu.add(item = new JMenuItem("Extra 1"));
        item.addActionListener(printListener);
        subMenu.add(subMenu2);

        // Assemble the Other menu itself.

        // Finally, add all the menus to the menu bar.
        add(testMenu);
        add(editMenu);
        add(otherMenu);
    }
}
